package com.guanyong.services;

import com.guanyong.dao.IVideoTopLogDaoMapper;
import com.guanyong.model.VideoPlayLogModel;
import com.guanyong.model.VideoTopLogModel;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/6/27 0027.
 */
@Service
public class VideoTopLogService {

    @Autowired
    IVideoTopLogDaoMapper iVideoTopLogDaoMapper;

    @Autowired
    ObjectClassTypeService objectClassTypeService;

    public ResultObject<Integer> AddVideoTopLog(VideoTopLogModel videoTopLogModel){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        if (iVideoTopLogDaoMapper.getobjectidCount(videoTopLogModel.getfObjectid())>0){
            resultObject.setCode(ResultCode.OperateFail);
            resultObject.setErrorMsg("电影已置顶");
        }else{
            videoTopLogModel.setfOrder(iVideoTopLogDaoMapper.getCount()+1);
            res =iVideoTopLogDaoMapper.insert(videoTopLogModel);
        }

        if (res >-1){
                resultObject.setCode(ResultCode.OperateOk);
        }else{
            resultObject.setCode(ResultCode.OperateFail);
            resultObject.setErrorMsg("置顶失败！");
        }
        return resultObject;
    }


    public Integer getVideoTopLogCount(Integer classType){

        return iVideoTopLogDaoMapper.getCount();
    }

    @Transactional
    public ResultObject<Integer> delVideoTopLog(Integer fid,Integer classType){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        VideoTopLogModel videoTopLogModel = iVideoTopLogDaoMapper.getByIdVideoTopLog(fid);
        if (videoTopLogModel!=null){
            Integer res1 = iVideoTopLogDaoMapper.ups(videoTopLogModel.getfOrder(),classType);
            Integer res2 = iVideoTopLogDaoMapper.del(fid);
            if (res1>-1 && res2>-1){
                resultObject.setCode(ResultCode.OperateOk);
            }else{
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultObject.setCode(ResultCode.OperateFail);
            }
        }else{
            resultObject.setCode(ResultCode.OperateFail);
        }

        return resultObject;
    }


    //2017年9月19日 17:22:41  修改完成
    //推荐列表
    public HashMap<String,Object> getVideoTopLogList(Integer classType,
                                                     Integer pno,
                                                     Integer pagesize,
                                                     Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iVideoTopLogDaoMapper.SelectVideoTopLogListCount(classType);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<VideoTopLogModel> videoTopLogModellList =iVideoTopLogDaoMapper.SelectVideoTopLogList
                (classType,(pno-1)*pagesize,pagesize);

        for (VideoTopLogModel videoTopLogModel :videoTopLogModellList){
            Object object= objectClassTypeService.GetObject(videoTopLogModel
                            .getfClasstype(),
                    videoTopLogModel.getfObjectid());
            if (object!=null) {
                videoTopLogModel.setfObject(object);
            }

        }
        hashMap.put("list",videoTopLogModellList);
             return hashMap;
    }

    @Transactional
    public ResultObject<Integer> upVideoTopLog(Integer fid,Integer classtype){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        Integer res=-1;
        VideoTopLogModel videoTopLogModel =iVideoTopLogDaoMapper.getByIdVideoTopLog(fid);
        if (videoTopLogModel.getfOrder()>1){
            VideoTopLogModel videoTopLogModeltop =iVideoTopLogDaoMapper.getByOrderVideoTopLog(videoTopLogModel.getfOrder()-1,classtype);
            Integer res2  =iVideoTopLogDaoMapper.down(videoTopLogModeltop.getfId());
            Integer res1  =iVideoTopLogDaoMapper.up(fid);
            if (res1>-1 && res2>-1){
                resultObject.setCode(ResultCode.OperateOk);
            }else{
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultObject.setCode(ResultCode.OperateFail);
            }
        }else{
            resultObject.setCode(ResultCode.OperateFail);
        }
        return resultObject;
    }
}
